import { Big } from "./Big"
import { GameMgr } from "../GameMgr"
import { Player } from "./Player"
import { Small } from "./Small"
import Utils from "../GameUtils"
import { 阵法 } from "../evolution/灵阵"

export class Position {
    x:number
    y:number
}
export enum Terrain {
    KONG = 0,        //空地
    SHAN = 1,        //山地
    SHUI = 2,        //河流
    LIN = 3,         //森林
    SHA = 4,         //沙漠
    JING = 5,        //水晶
}
/** 印记 */
export class Mark {
    type: 阵法
    big: Big    //放阵法的大子
}
export class Point {

    gameMgr:GameMgr
    position:Position = new Position()
    terrain:Terrain
    id:string
    small:Small = null
    big:Big = null
    arr_marks = new Set<Mark>()
    is_敲山震虎_flag = false    //敲山震虎 递归用到

    constructor(gameMgr:GameMgr,x:number,y:number,terrain:Terrain) {
        this.gameMgr = gameMgr
        this.position.x = x
        this.position.y = y
        this.terrain = terrain
        this.id = Utils.getPointId(x,y)
    }

    gc() {
        this.gameMgr = null
        this.small = null
        this.big = null
        this.arr_marks = null
    }
    
    toObj(){
        let obj = {
            "id":this.id,
            "position":this.position,
            "terrain":this.terrain,
            "arr_marks":this.markToJson()
        }
        return obj
    }

    update() {
        this.gameMgr.sendBroadcast(Utils.getObj("update_point",{"point":this.toObj()},true,false),false)
    }

    changeTerrain(newTerrain: Terrain) {
        this.terrain = newTerrain
        this.update()
    }

    /**
     * 客户端知道是哪个玩家的阵法就够了
     */
    markToJson() {
        let arr = []
        for (let mark of this.arr_marks) {
            arr.push({"type":mark.type, "username":mark.big.player.username})
        }
        return arr
    }

    addMark(type: 阵法, big: Big) {
        let mark = new Mark()
        mark.big = big
        mark.type = type
        this.arr_marks.add(mark)
        this.update()
    }

    removeMark(big: Big) {
        for (let mark of this.arr_marks) {
            if (mark.big == big) {
                this.arr_marks.delete(mark)
            }
        }
        this.update()
    }

    /**
     * 是否有这个印记
     */
    hasMark(type:阵法, player:Player) {
        for (let mark of this.arr_marks) {
            if (type == mark.type && mark.big.player == player) {
                return true
            }
        }
        return false
    }

    /**
     * 获取一个不是自己的印记
     */
    getMarkNotMe(type:阵法, player:Player) {
        for (let mark of this.arr_marks) {
            if (type == mark.type && mark.big.player != player) {
                return mark
            }
        }
        return null
    }
}
